package edu.ncu.service.impl;

import edu.ncu.dao.UserDao;
import edu.ncu.entity.User;
import edu.ncu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("userService")
public class UserServiceImpl implements UserService {
    private final UserDao userDao;

    @Autowired
    public UserServiceImpl(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public List<User> findAll() {
        System.out.println("进入了service");
        return userDao.findAll();
    }

    @Override
    public List<User> findByFuzzy(String account, String uname) {

        account = account == null ? null : "%"+account+"%";
        uname = uname == null ? null : "%"+uname+"%";
        System.out.println("service处理后的account和uname："+ account + "----" + uname);
        return userDao.findByFuzzy(account, uname);
    }

    /**
     * 判断账号密码是否正确
     * @param account
     * @param upwd
     * @return
     */
    @Override
    public Boolean judgePwd(String account, String upwd) {
        User userByAccount = userDao.findByAccount(account);
        //账户存在并且密码正确返回true
        if(userByAccount!=null)
        {
            return userByAccount.getUpwd().equals(upwd);
        }
        return false;
    }

    @Override
    public User findByUserId(int userId) {
        return userDao.findByUserId(userId);
    }

    @Override
    public User findByAccount(String account) {
        return userDao.findByAccount(account);
    }

    @Override
    public void updateUserState(int state, int userId) {
        userDao.updateUserState(state, userId);
    }

    @Override
    public void updateUserHead(String headPhoto, int userId) {
        userDao.updateUserHead(headPhoto, userId);
    }

    @Override
    public void saveUser(User user) {
        userDao.saveUser(user);
    }

    @Override
    public void updateUser(User user) {
        userDao.updateUser(user);
    }

    @Override
    public List<User> findLikeAccount(String account) {
        return userDao.findLikeAccount(account) ;
    }
}
